package co.yixiang.modules.system.rest;
import java.util.Arrays;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.template.domain.YxSystemCity;
import co.yixiang.modules.template.service.YxSystemCityService;
import co.yixiang.modules.template.service.dto.YxSystemCityDto;
import co.yixiang.modules.template.service.dto.YxSystemCityQueryCriteria;
import co.yixiang.modules.user.domain.Area;
import co.yixiang.modules.user.service.AreaService;
import co.yixiang.modules.user.service.dto.AreaDto;
import co.yixiang.modules.user.service.dto.AreaQueryCriteria;
import lombok.AllArgsConstructor;
import co.yixiang.logging.aop.log.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import co.yixiang.domain.PageResult;
/**
 * @author lsh
 * @date 2021-04-19
 */
@AllArgsConstructor
@Api(tags = "area管理")
@RestController
@RequestMapping("/api/area")
public class AreaController {

    private final AreaService areaService;
    private final YxSystemCityService yxSystemCityService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, YxSystemCityQueryCriteria criteria) throws IOException {
        areaService.download(generator.convert(yxSystemCityService.queryAll(criteria), AreaDto.class), response);
    }

    @GetMapping
    @Log("查询area")
    @ApiOperation("查询area")
    public ResponseEntity<PageResult<YxSystemCityDto>> getAreas(@RequestBody YxSystemCityQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxSystemCityService.query4All(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value ="operate")
    @Log("查询area")
    @ApiOperation("查询area")
    public ResponseEntity<PageResult<YxSystemCityDto>> getAreas4operate(YxSystemCityQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxSystemCityService.query4operate(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增area")
    @ApiOperation("新增area")
    public ResponseEntity<Object> create(@Validated @RequestBody YxSystemCity resources){
        return new ResponseEntity<>(yxSystemCityService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改area")
    @ApiOperation("修改area")
    public ResponseEntity<Object> update(@Validated @RequestBody YxSystemCity resources){
        yxSystemCityService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除area")
    @ApiOperation("删除area")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxSystemCityService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
